package com.com.effectivejava.generics.examples;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by User on 15/07/2014.
 */
public class SafeWork {

    // Using wild card, you can only read the contents of the sets.
    // inside following method you cannot modify the collection.
    static int numElementsInCommon(Set<?> s1, Set<?> s2) {
        int result = 0;
        for (Object o1 : s1)
            if (s2.contains(o1))
                result++;
        return result;
    }

    public static void main(String[] args) {
        Set s1 = new HashSet();
        Set s2 = new HashSet();

        s1.add(1);s1.add(2);s1.add(3);s1.add(4);
        s1.add(0);s2.add(2);s2.add(6);s2.add(4);

        System.out.println("intersection of sets = " + numElementsInCommon(s1, s2));
    }

    // You cannot modify this collection object. as compiler is stopping you from corrupting the collection.

    static void addIn(Collection<? extends Object> c, Object o){
         c.add(null);
    }


}
